Ext.onReady(function(){
	
	/**
	 * 取合同ID
	 */
	function getTrainingContractId(){
		var selected = grid.getSelectionModel().getSelection();
		var ids="";// 合同ID
		Ext.each(selected, function (item) {
			ids+=item.data.id;
		});
		return ids ;
	}
	
    Ext.define('json_data',{
        extend: 'Ext.data.Model',
        fields: [
			 {name:'id',type:'string'},
			 {name:'name',type:'string'},
             {name:'student_id',type:'string'},
             {name:'student__name',type:'string'},
             {name:'service_fee',type:'string'},
             {name:'education_fee',type:'string'},
             {name:'traffic_fee',type:'string'},
             {name:'service_fee_pay',type:'string'},
             {name:'education_fee_pay',type:'string'},
             {name:'traffic_fee_pay',type:'string'},
             {name:'service_fee_card',type:'string'},
             {name:'education_fee_card',type:'string'},
             {name:'traffic_fee_card',type:'string'},
             {name:'education_ratio',type:'string'},
             {name:'tran_type',type:'string'},
             {name:'tran_num',type:'string'},
             {name:'tran_last',type:'string'},
             {name:'begin_time',type:'string'},
             {name:'end_time',type:'string'},
             {name:'finish_time',type:'string'},
             {name:'contract_type',type:'string'},
             {name:'state',type:'string'},
             {name:'audit_man_id',type:'string'},
             {name:'audit_man__name',type:'string'},
             {name:'finance_audit_man__name',type:'string'},
             {name:'audit_remark',type:'string'},
             {name:'finance_audit_remark',type:'string'},
             {name:'remark',type:'string'},
             {name:'student__company__cn_name',type:'string'},
             {name:'entering_man__name',type:'string'},
             
          // 合同明细
             {name:'training_contract_id',type:'string'},
             {name:'grade_id',type:'string'},
             {name:'grade__name',type:'string'},
             {name:'course_id',type:'string'},
             {name:'course__name',type:'string'},
             {name:'study_type',type:'string'},
             {name:'course_times',type:'string'},
             {name:'old_price',type:'string'},
             {name:'current_price',type:'string'},
             {name:'rebate',type:'string'},
             {name:'total_prices',type:'string'},
             {name:'pay_prices',type:'string'},
             {name:'pay_card_prices',type:'string'},
             {name:'course_times_week',type:'string'},

          	 // 合同签订人明细
             {name:'employe_id',type:'string'},
             {name:'employe__name',type:'string'},
             {name:'ratio',type:'string'},
             {name:'ret_ratio',type:'string'}
        ]
    });
    //创建数据源
    var store = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/customercontrol/training_contract_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: false
    });
    //创建数据源
    var storeDetail = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/customercontrol/training_contract_detail_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: true
    });
    //创建数据源
    var storeMan = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/customercontrol/training_contract_mans_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: true
    });

    //年级combox
    var gradeComboBox= Ext.create('Ext.form.field.ComboBox', { 
	   name:'grade_id',
       fieldLabel: '年级', 
       editable:false,
       store: new Ext.data.Store({ 
           singleton : true, 
	   	   proxy: {
	   	        type: 'ajax',
	   	        url: '/customercontrol/grade_select/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'name'],
           autoLoad:true 
       }), 
       loadingText:'正在加载数据，请稍侯……', 
       triggerAction:'all', 
       valueField:'id', 
       displayField:'name'
    }); 


  	//课程combox --签订人
    var courseComboBoxMan= Ext.create('Ext.form.field.ComboBox', {
	   name:'course_id',
       fieldLabel: '课程', 
       editable:false,
       store: new Ext.data.Store({ 
           singleton : true, 
	   	   proxy: {
	   	        type: 'ajax',
	   	        url: '/customercontrol/course_select/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'name'],
           autoLoad:true 
       }), 
       loadingText:'正在加载数据，请稍侯……', 
       triggerAction:'all', 
       valueField:'id', 
       displayField:'name'
    }); 
    
    //课程combox -- 合同明细
    var courseComboBoxDetail= Ext.create('Ext.form.field.ComboBox', {
    	name:'course_id',
    	fieldLabel: '课程', 
    	editable:false,
    	store: new Ext.data.Store({ 
    		singleton : true, 
    		proxy: {
    		type: 'ajax',
    		url: '/customercontrol/course_select/',
    		actionMethods:'post',
    		reader: {
    		type: 'json'
    	}
    	},
    	fields:['id', 'name'],
    	autoLoad:true 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'name'
    }); 
    
  	//员工combox
    var employeComboBox= Ext.create('Ext.form.field.ComboBox', { 
       id : 'employe_combobox_id',
	   name:'employe_id',
       fieldLabel: '员工', 
       typeAhead:true,
       minChars:1,
       editable:true,
       store: new Ext.data.Store({ 
           singleton : true, 
	   	   proxy: {
	   	        type: 'ajax',
	   	        url: '/employecontrol/employe_select/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'name'],
           autoLoad:true 
       }), 
       loadingText:'正在加载数据，请稍侯……', 
       triggerAction:'all', 
       valueField:'id', 
       displayField:'name',
       listeners: {
    		'select': function(combo, records, opts) {	
   	    		//alert("待增加学生查找") ;
    		},
    		'beforequery' : function(queryEvent,eOpts){
				queryEvent.combo.getStore().load();
			} 

    	}
    });
    
    
    //学生combox
    var studentComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id:'student_combobox_id',
    	name:'student_id',
    	emptyText: '请选择学生',
    	labelWidth: 30,
    	width : 120,
    	fieldLabel: '学生', 
    	typeAhead:true,
        minChars:1,
    	editable:true,
    	store: new Ext.data.Store({ 
    		singleton : true, 
    		proxy: {
    		type: 'ajax',
    		url: '/customercontrol/company_student_select/',
    		actionMethods:'post',
    		reader: {
    		type: 'json'
    	}
    	},
    	fields:['id', 'name'],
    	autoLoad:false 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'name',
    	listeners: {
    		'beforequery' : function(queryEvent,eOpts){
				 queryEvent.combo.getStore().load();
			} 
		}
    }); 
	
  //合同状态combox
    var contractStateComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id:'contract_state_combobox_id',
    	name:'contract_state_id',
    	emptyText: '请选择合同状态',
    	labelWidth: 60,
    	width : 180,
    	fieldLabel: '合同状态', 
    	editable:false,
    	store: new Ext.data.Store({ 
			fields:['id','value'], 
			data:[
				//{'id':'' ,'value':'全部'}, 
				//{'id':'0','value':'预制'},
				{'id':'1','value':'有效'},
				//{'id':'2','value':'主管待审核'},
				{'id':'3','value':'财务待审核'},
				//{'id':'5','value':'财务驳回'},
				//{'id':'6','value':'主管驳回'},
				{'id':'4','value':'归档'}
			],
	    	autoLoad:false 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'value'
    }); 
    
    
    
    //多选
    var selModel = Ext.create('Ext.selection.CheckboxModel');
    //创建Grid
    var grid = Ext.create('Ext.grid.Panel',{
        store: store,
        //height: 500,
        //selModel:selModel,
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
            {text: "ID", width: 100, dataIndex: 'id', sortable: false,hidden : true},
			{text: "合同编码", width: 90, dataIndex: 'name', sortable: false},
            {text: "学生姓名ID", width: 100, dataIndex: 'student_id', sortable: false,hidden : true},
            {text: "校区", width: 80, dataIndex: 'student__company__cn_name', sortable: false},
            {text: "学生姓名", width: 55, dataIndex: 'student__name', sortable: false},
            {text: "状态 ", width: 35, dataIndex: 'state', sortable: false,
            	renderer:function(v){
        		if (v==0) 
                	return '预制'
                else if (v==1)
                    return '有效'   
                else if (v==2)
                	return '主管待审核'   
    			else if (v==3)
        			return '财务待审核'
    			else if (v==5)
    				return '财务驳回'
				else if (v==6)
					return '主管驳回'
        		else
        			return '归档'
            	}
            },
            {text: "合同类型", width: 55, dataIndex: 'contract_type', sortable: false,
            	renderer:function(v){
            	if (v==1) 
                	return '新签'
                else if (v==2)
                    return '续费'   
                else if(v==3)
        			return '<font color="red">退费</font>'
        		else
        			return '转介绍'
            	}},
            {
            	text: '综合服务费',
	            columns: [
	            	{text: "应收", width: 65, dataIndex: 'service_fee', sortable: false},
	            	{text: "现金", width: 65, dataIndex: 'service_fee_pay', sortable: false},
	            	{text: "刷卡", width: 65, dataIndex: 'service_fee_card', sortable: false}
	            ]
            },
            {
            	text: '教育咨询费',
	            columns: [
	            	{text: "应收", width: 65, dataIndex: 'education_fee', sortable: false},
	            	{text: "现金", width: 65, dataIndex: 'education_fee_pay', sortable: false},
	            	{text: "刷卡", width: 65, dataIndex: 'education_fee_card', sortable: false}
	            	//{text: "折扣", width: 65, dataIndex: 'education_ratio', sortable: false}
	            ]
            },
            {
            	text: '交通费',
	            columns: [
	            	{text: "应收", width: 65, dataIndex: 'traffic_fee', sortable: false},
	            	{text: "现金", width: 65, dataIndex: 'traffic_fee_pay', sortable: false},
	            	{text: "刷卡", width: 65, dataIndex: 'traffic_fee_card', sortable: false}
	            ]
            },
            {text: "接送方式", width: 60, dataIndex: 'tran_type', sortable: false,hidden : true,
            	renderer:function(v){
            	if (v==1) 
                	return '专车'  
    			else if(v==2)
        			return '校车'
        		else 
        			return '其它'
            	}
            },
            {text: "接送次数", width: 60, dataIndex: 'tran_num', sortable: false,hidden : true},
            {text: "接送期限", width: 80, dataIndex: 'tran_last', sortable: false,hidden : true},
            {text: "合同生效时间 ", width: 80, dataIndex: 'begin_time', sortable: false},
            {text: "合同失效时间 ", width: 80, dataIndex: 'end_time', sortable: false},
            {text: "合同签定时间", width: 80, dataIndex: 'finish_time', sortable: false},
            {text: "录入人", width: 70, dataIndex: 'entering_man__name', sortable: false},
            {text: "复核人", width: 70, dataIndex: 'audit_man__name', sortable: false},
            {text: "终审人", width: 70, dataIndex: 'finance_audit_man__name', sortable: false},
            {text: "复核意见", width: 100, dataIndex: 'audit_remark', sortable: false},
            {text: "终审意见", width: 100, dataIndex: 'finance_audit_remark', sortable: false},
            {text: "合同备注", width: 100, dataIndex: 'remark', sortable: false}
        ],
        title: '学生合同审核',
        // disableSelection: false,
        layout : 'fit',
        //frame:true,
        loadMask:true,
        //region: 'north',
        //renderTo: 'customer-contract-grid',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				gridDetail.enable() ;
    				gridMan.enable() ;
//    				Ext.getCmp('mainEditButton').enable() ;
//    				Ext.getCmp('mainDelButton').enable() ;
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	var selected = grid.getSelectionModel().getSelection();
                	trainingContractForm.getForm().reset();
            		trainingContractForm.loadRecord(selected[0]);
            		trainingContractWin.show();
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[{
        		xtype: 'combobox',
        		labelWidth: 30,
        		width : 120,
        		id:'company_combox_id',
            	name:'company',
               	fieldLabel: '校区',
               	emptyText: '请选择校区',
               	editable:false,
               	store: new Ext.data.Store({
                   	singleton : true, 
        	   	    proxy: {
        	   	        type: 'ajax',
        	   	        url: '/resourcecontrol/company_select_43/',
        	   	        actionMethods:'post',
        	   	        reader: {
        	   	            type: 'json'
        	   	        }
        	   	    },
                   fields:['id', 'cn_name'], 
                   autoLoad:true
               	}), 
               	loadingText:'正在加载数据，请稍侯……', 
               	triggerAction:'all', 
               	valueField:'id', 
               	displayField:'cn_name',
               	listeners: {
            		'select': function(combo, records, opts) {	
    	   	    		var params = { company_id : Ext.getCmp('company_combox_id').getValue() };
    	   	    		Ext.apply(studentComboBox.getStore().proxy.extraParams, params) ;
    	   	    		studentComboBox.getStore().load();
            		}
            	}
        	},/*studentComboBox*/contractStateComboBox,{
        		xtype:'button',
        		text:'查询',
        		iconCls:'search', 
        		handler:function(){
//	        		var sid = Ext.getCmp('student_combobox_id').getValue() ;
//	    			if(sid!=null){
//	    				var params = { student_id: Ext.getCmp('student_combobox_id').getValue() };
//	        		    Ext.apply(store.proxy.extraParams, params);
//	        		    store.load();
//	    			}else{
//	    				Ext.Msg.alert('系统提示', '请选择学生!');
//	    			}
//        			if(Ext.getCmp('company_combox_id').getValue()==''){
//        				Ext.Msg.alert('系统提示', '请选择具体校区!');
//        			}else{
        			if(Ext.getCmp('company_combox_id').getValue()!=null && Ext.getCmp('contract_state_combobox_id').getValue()!=null ){
	    				var params = { company_id:Ext.getCmp('company_combox_id').getValue(),state: Ext.getCmp('contract_state_combobox_id').getValue() };
		    		    Ext.apply(store.proxy.extraParams, params);
		    		    store.currentPage=1;
		    		    store.load();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择校区及状态!');
	    			}
//        			}
        			
        		}
        	},/*{
        		xtype:'button',
        		text:'添加',
        		handler:function(){
        			var sid = Ext.getCmp('student_combobox_id').getValue() ;
        			if(sid!=null){
        				Ext.Ajax.request({
            			    url:'/customercontrol/training_contract_list/',
            			    method : 'post',
            			    params:{
        						student_id:sid,start:0,limit:20
            			    },
            			    success:function(response){
            			        var results =  Ext.decode(response.responseText);
            			        if(results.totalCount==0){
            			        	trainingContractForm.getForm().reset();
                        			Ext.getCmp('trainingContractForm').getForm().setValues({student_id:sid});
                        			trainingContractWin.show();
            			        }else{
            			        	Ext.Msg.alert('系统提示', '不能重复预制学生合同!');
            			        }
            			    }
            			});
        				
        				
        				
        			}else{
        				Ext.Msg.alert('系统提示', '请选择学生!');
        			}
	        		
        		}
        	},*/{
        		xtype:'button',
        		id:'mainEditButton',
        		text:'详情',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
        			if(selected!=''){
        				trainingContractForm.getForm().reset();
        				trainingContractForm.loadRecord(selected[0]);
            			trainingContractWin.show();
        			}else{
        				Ext.Msg.alert('系统提示', '请选择需要查看详情的合同!');
        			}
        		}
        	},{
        		xtype:'button',
        		text:'审核通过',
        		iconCls:'edit', 
        		handler:function(){
	        		var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				var ids="";// 合同ID
		    			var state="" ;
		    			Ext.each(selected, function (item) {
		    				ids+=item.data.id;
		    				state+=item.data.state;
		    			});
		    			
		    			if(state==3){
		    				auditForm.getForm().reset();
		    				auditForm.loadRecord(selected[0]);
			    			Ext.getCmp('auditForm').getForm().setValues({training_contract_id:ids, state:'1'});
			    			auditWin.show();
		    			}else{
		    				Ext.Msg.alert('系统提示', '您无权对当前合同状态进行审核!');
		    			}
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要审核的合同!');
	    			}
        		
        		
	        		
	    			
        		}
        	},{
        		xtype:'button',
        		text:'审核驳回',
        		iconCls:'edit', 
        		handler:function(){
	        		var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				var ids="";// 合同ID
		    			var state="" ;
		    			Ext.each(selected, function (item) {
		    				ids+=item.data.id;
		    				state+=item.data.state;
		    			});
		    			
		    			if(state==3){
		    				auditForm.getForm().reset();
		    				auditForm.loadRecord(selected[0]);
			    			Ext.getCmp('auditForm').getForm().setValues({training_contract_id:ids, state:'5'});
			    			auditWin.show();
		    			}else{
		    				Ext.Msg.alert('系统提示', '您无权对当前合同状态进行审核驳回!');
		    			}
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要审核驳回的合同!');
	    			}
        		
        		
	        		
	    			
        		}
        	},{
        		xtype:'button',
        		text:'审核归档',
        		iconCls:'edit', 
        		handler:function(){
	        		var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				var ids="";// 合同ID
		    			var state="" ;
		    			Ext.each(selected, function (item) {
		    				ids+=item.data.id;
		    				state+=item.data.state;
		    			});
		    			
		    			if(state==1){
		    				auditForm.getForm().reset();
		    				auditForm.loadRecord(selected[0]);
			    			Ext.getCmp('auditForm').getForm().setValues({training_contract_id:ids, state:'4'});
			    			auditWin.show();
		    			}else{
		    				Ext.Msg.alert('系统提示', '您无权对当前合同状态进行审核归档!');
		    			}
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要审核归档的合同!');
	    			}
        		
        		
	        		
	    			
        		}
        	}/*,{
        		xtype:'button',
        		disabled:true,
        		id:'mainDelButton',
        		text:'删除',
        		handler:function(){
	        		var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
	        				if(btn=='yes'){
			    				var ids="";
			        			Ext.each(selected, function (item) {
			        				ids+=item.data.id+",";
			        			});
			        			Ext.Ajax.request({
			        			    url:'/customercontrol/training_contract_del/',
			        			    method : 'post',
			        			    params:{
			        			    	ids:ids
			        			    },
			        			    success:function(response){
			        			        var results =  Ext.decode(response.responseText);
			        			        if(results.success){
			        			        	store.load();
			        			        }else{
			        			        	Ext.Msg.alert('系统提示', results.message);
			        			        }
			        			    }
			        			});
	        				}
	        			});
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要删除的合同!');
	    			}
        		}
        	}*/]
        }]
    });
    


    
    // 审核
    var auditForm = Ext.create('Ext.form.Panel', {
		id:'auditForm',
		width: 550,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'合同ID',	
			name:'training_contract_id'
		},{
			xtype:'hiddenfield',
			fieldLabel:'状态',	
			name:'state'
		},
		{
			xtype:'textareafield',
			fieldLabel:'审核意见',
			allowBlank : false,
			name:'finance_audit_remark'
		}]
	});

    
    var auditWin = Ext.create('Ext.window.Window', {
		title : '审核',
		modal: true,
		closeAction: 'hide',
		items : [auditForm],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (auditForm.getForm().isValid()) {
					auditForm.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/customercontrol/training_contract_update_state2/',
						success : function(form, action) {
							auditWin.hide();
							store.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				auditForm.getForm().reset();
				auditWin.hide();
			}
		} ]
	});   



 // 合同
	var trainingContractForm = Ext.create('Ext.form.Panel', {
		id:'trainingContractForm',
		width: 750,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'id',
			name:'id'
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				xtype : 'hiddenfield',
				fieldLabel : '学生ID',
				name : 'student_id',
				allowBlank : false,
				blankText : '请输入'
			}, {
				xtype : 'hiddenfield',
				fieldLabel : '是否参照',
				name : 'is_like',
				allowBlank : false,
				blankText : '请输入'
			}, {
				xtype : 'hiddenfield',
				fieldLabel : '参照合同ID',
				name : 'like_id',
				allowBlank : false,
				blankText : '请输入'
			}, {
				xtype : 'numberfield',
				value : '0',
				fieldLabel : '应收综合服务费',
				name : 'service_fee',
				allowBlank : false,
				blankText : '请输入'
			},{
				fieldLabel : '现金',
				xtype : 'numberfield',
				value : '0',
				name : 'service_fee_pay',
				allowBlank : true,
				blankText : '请输入'
			},{
				fieldLabel : '刷卡',
				xtype : 'numberfield',
				value : '0',
				name : 'service_fee_card',
				allowBlank : true,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '应收教育咨询费',
				xtype : 'numberfield',
				value : '0',
				name : 'education_fee',
				allowBlank : false,
				blankText : '请输入'
			}, {
				fieldLabel : '现金',
				name : 'education_fee_pay', 
				xtype : 'numberfield',
				value : '0',
				allowBlank : false,
				blankText : '请输入'
			}, {
				fieldLabel : '刷卡',
				name : 'education_fee_card', 
				xtype : 'numberfield',
				value : '0',
				allowBlank : false,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '应收交通费',
				xtype : 'numberfield',
				value : '0',
				name : 'traffic_fee', 
				allowBlank : true,
				blankText : '请输入'
			}, {
				fieldLabel : '现金',
				xtype : 'numberfield',
				value : '0',
				name : 'traffic_fee_pay',
				allowBlank : true,
				blankText : '请输入'
			}, {
				fieldLabel : '刷卡',
				xtype : 'numberfield',
				value : '0',
				name : 'traffic_fee_card',
				allowBlank : true,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[{
				xtype:'combobox',
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '接送方式',
				name : 'tran_type',
				allowBlank : false,
				blankText : '请输入',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
						{'id':'1','value':'专车'},
						{'id':'2','value':'校车'},
						{'id':'3','value':'其它'},
					]
				}),
				columnWidth: 1/2,
				listeners: {
	        		'select': function(combo, records, opts) {
	        			Ext.getCmp('trainingContractForm').getForm().setValues({tran_num:'0'});
	        		}
        		}
			},{
				fieldLabel : '接送次数',
				name : 'tran_num',
				allowBlank : true,
				blankText : '请输入'
			},{
				fieldLabel : '接送期限',
				xtype:'datefield',
				format: 'Y-m-d',
				name : 'tran_last', 
				editable:false,
				allowBlank : true,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				xtype:'datefield',
				fieldLabel : '合同签定时间',
				name : 'finish_time',
				xtype:'datefield',
				format: 'Y-m-d',
				allowBlank : false,
				editable:false,
				blankText : '请输入'
			},{
				fieldLabel : '合同生效时间',
				xtype:'datefield',
				format: 'Y-m-d',
				name : 'begin_time',
				allowBlank : false,
				editable:false,
				blankText : '请输入'
			}, {
				fieldLabel : '合同失效时间 ',
				xtype:'datefield',
				format: 'Y-m-d',
				name : 'end_time', 
				allowBlank : false,
				editable:false,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'datefield',
			items:[ {
				fieldLabel : '合同编码',
				xtype:'textfield',
				name : 'name',
				allowBlank : false,
				blankText : '请输入'
			},{
				xtype:'combobox',
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '合同类型',
				name : 'contract_type',
				allowBlank : false,
				blankText : '请输入',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
						{'id':'1','value':'新签'},
						{'id':'2','value':'续费'},
						{'id':'3','value':'退费'},
						{'id':'4','value':'转介绍'}
					]
				}),
				columnWidth: 1/2
			}]
		},{
			xtype:'textareafield',
			fieldLabel:'备注',
			name:'remark'
		}]
	});


	var trainingContractWin = Ext.create('Ext.window.Window', {
		title : '学生合同详情',
		modal: true,
		closeAction: 'hide',
		items : [trainingContractForm],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ /*{
			text : '保存',
			handler : function() {
				if (trainingContractForm.getForm().isValid()) {
					trainingContractForm.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/customercontrol/training_contract_save/',
						success : function(form, action) {
							trainingContractWin.hide();
							store.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		},*/ {
			text : '确定',
			handler : function() {
				trainingContractForm.getForm().reset();
				trainingContractWin.hide();
			}
		} ]
	});    

	
	

	//---合同明细
	var gridDetail = Ext.create('Ext.grid.Panel',{
		disabled:true,
        store: storeDetail,
        //height: 500,
        //selModel: Ext.create('Ext.selection.CheckboxModel'),
        columnLines:true,
        columns: [
                  Ext.create('Ext.grid.RowNumberer'),// 序列
      			{text: "ID", width: 100, dataIndex: 'id', sortable: false,hidden : true},
      			{text: "合同ID", width: 100, dataIndex: 'training_contract_id', sortable: false,hidden : true},
                  {text: "年级ID", width: 100, dataIndex: 'grade_id', sortable: false,hidden : true},
                  {text: "年级", width: 40, dataIndex: 'grade__name', sortable: false},
                  {text: "课程ID", width: 120, dataIndex: 'course_id', sortable: false,hidden : true},
                  {text: "课程", width: 80, dataIndex: 'course__name', sortable: false},
                  {text: "学习方式", width: 60, dataIndex: 'study_type', sortable: false,
                  	renderer:function(v){
	                  	if (v==1) 
	                		return '一对一'  
		                else if(v==2)
		                    return '一对二' 
		                else if(v==3)
		                    return '一对三'    
		                else if(v==4)
		                	return '一对四'    
		    			else if(v==5)
		        			return '一对五'
		        		else if(v==6)
		        			return '户外'
      	            }
                  },
                  {text: "总课时", width: 60, dataIndex: 'course_times', sortable: false},
                  {text: "原价", width: 60, dataIndex: 'old_price', sortable: false},
                  {text: "当前购买价格", width: 80, dataIndex: 'current_price', sortable: false},
                  {text: "折扣", width: 60, dataIndex: 'rebate', sortable: false},
                  {text: "总价", width: 60, dataIndex: 'total_prices', sortable: false},
                  {text: "已付现金", width: 60, dataIndex: 'pay_prices', sortable: false},
                  {text: "已付刷卡", width: 60, dataIndex: 'pay_card_prices', sortable: false},
                  {text: "每周安排课时", width: 80, dataIndex: 'course_times_week', sortable: false}
              ],
        title: '合同明细',
        // disableSelection: false,
        layout : 'fit',
        //frame:true,
        loadMask:true,
//        region: 'north',
//        renderTo: 'customer-tab',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	var selected = gridDetail.getSelectionModel().getSelection();
    				formDetail.getForm().reset();
					formDetail.loadRecord(selected[0]);

					var selected = grid.getSelectionModel().getSelection();
					var sids="";// 学生ID
        			Ext.each(selected, function (item) {
        				sids+=item.data.student_id;
        			});
        			Ext.getCmp('formDetail').getForm().setValues({student_id:sids});
        			winDetail.show();
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[/*{
        		xtype:'button',
        		text:'测试-需要修改为TAB激活',
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
        			var ids="";// 合同ID
        			var sids="";// 学生ID
        			Ext.each(selected, function (item) {
        				ids+=item.data.id;
        				sids+=item.data.student_id;
        			});
        			//alert(ids) ;
        			Ext.getCmp('formDetail').getForm().setValues({training_contract_id:ids});
        			Ext.getCmp('formDetail').getForm().setValues({student_id:sids});
        			var params = {student_id: ids};
        		    Ext.apply(storeDetail.proxy.extraParams, params);
        		    storeDetail.load();
        		}
        	},{
        		xtype:'button',
        		text:'添加',
        		handler:function(){
        			formDetail.getForm().reset();
        			// 设置合同ID
        			var selected = grid.getSelectionModel().getSelection();
        			var ids="";// 合同ID
        			var sids="";// 学生ID
        			Ext.each(selected, function (item) {
        				ids+=item.data.id;
        				sids+=item.data.student_id;
        			});
        			//alert(ids) ;
        			Ext.getCmp('formDetail').getForm().setValues({training_contract_id:ids});
        			Ext.getCmp('formDetail').getForm().setValues({student_id:sids});
        			winDetail.show();
        		}
        	},*/{
        		xtype:'button',
        		text:'详情',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = gridDetail.getSelectionModel().getSelection();
        			if(selected!=''){
        				formDetail.getForm().reset();
    					formDetail.loadRecord(selected[0]);

    					var selected = grid.getSelectionModel().getSelection();
    					var sids="";// 学生ID
            			Ext.each(selected, function (item) {
            				sids+=item.data.student_id;
            			});
            			Ext.getCmp('formDetail').getForm().setValues({student_id:sids});
            			winDetail.show();
        			}else{
        				Ext.Msg.alert('系统提示', '请选择需要查看详情的合同明细!');
        			}
        		}
        	}/*,{
        		xtype:'button',
        		text:'删除',
        		handler:function(){
        			var selected = gridDetail.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
	        				if(btn=='yes'){
	        					var ids="";
	                			Ext.each(selected, function (item) {
	                				ids+=item.data.id+",";
	                			});
	                			Ext.Ajax.request({
	                			    url:'/customercontrol/training_contract_detail_del/',
	                			    method : 'post',
	                			    params:{
	                			    	ids:ids
	                			    },
	                			    success:function(response){
	                			        var results =  Ext.decode(response.responseText);
	                			        if(results.success){
	                			        	storeDetail.load();
	                			        }else{
	                			        	Ext.Msg.alert('系统提示', results.message);
	                			        }
	                			    }
	                			});
	        				}
	        			});
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要删除的合同明细!');
	    			}
        			
        		}
        	}*/]
        }]
    });
    
	var formDetail = Ext.create('Ext.form.Panel', {
		id:'formDetail',
		width: 350,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'id',
			name:'id'
		},{
			xtype:'hiddenfield',
			fieldLabel:'合同ID',
			allowBlank : false,
			name:'training_contract_id'
		},{
			xtype:'hiddenfield',
			fieldLabel:'学生ID',
			allowBlank : false,
			name:'student_id'
		},gradeComboBox,
		courseComboBoxDetail,//
		{
			xtype:'combobox',
			editable:false,
			valueField:'id',
			displayField:'value',
			queryMode:'local',
			fieldLabel : '学习方式',
			name : 'study_type',
			allowBlank : false,
			blankText : '请输入',
			store:Ext.create('Ext.data.Store',{
				fields:['id','value'], 
				data:[
					{'id':'1','value':'一对一'},
					{'id':'2','value':'一对二'},
					{'id':'3','value':'一对三'},
					{'id':'4','value':'一对四'},
					{'id':'5','value':'一对五'},
					{'id':'6','value':'户外'}
				]
			})
		},{
			xtype:'textfield',
			fieldLabel:'总课时',
			name:'course_times'
		},{
			xtype:'textfield',
			fieldLabel:'原价',
			name:'old_price'
		},{
			xtype:'textfield',
			fieldLabel:'当前购买价格',
			name:'current_price'
		},{
			xtype:'textfield',
			fieldLabel:'已付现金',
			name:'pay_prices'
		},{
			xtype:'textfield',
			fieldLabel:'已付刷卡',
			name:'pay_card_prices'
		},{
			xtype:'textfield',
			fieldLabel:'每周安排课时',
			name:'course_times_week'
		}]
	});

	
	var winDetail = Ext.create('Ext.window.Window', {
		title : '合同明细',
		modal: true,
		closeAction: 'hide',
		items : [formDetail],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ /*{
			text : '保存',
			handler : function() {
				if (formDetail.getForm().isValid()) {
					formDetail.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/customercontrol/training_contract_detail_save/',
						success : function(form, action) {
							winDetail.hide();
							storeDetail.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		},*/ {
			text : '确定',
			handler : function() {
				formDetail.getForm().reset();
				winDetail.hide();
			}
		} ]
	});
	

	//---合同签订人明细
	var gridMan = Ext.create('Ext.grid.Panel',{
		disabled:true,
        store: storeMan,
        //height: 500,
        //selModel: Ext.create('Ext.selection.CheckboxModel'),
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 100, dataIndex: 'id', sortable: false,hidden : true},
			{text: "合同ID", width: 100, dataIndex: 'training_contract_id'},
            {text: "员工ID", width: 100, dataIndex: 'employe_id', sortable: false,hidden : true},
            {text: "员工", width: 100, dataIndex: 'employe__name', sortable: false},
            {text: "提成比例(%)", width: 120, dataIndex: 'ratio', sortable: false},
            {text: "退费罚款比例(%)", width: 120, dataIndex: 'ret_ratio', sortable: false}
        ],
        title: '合同签订人明细',
        // disableSelection: false,
        layout : 'fit',
        //frame:true,
        loadMask:true,
//        region: 'north',
//        renderTo: 'customer-tab',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	var selected = gridMan.getSelectionModel().getSelection();
    				formMan.getForm().reset();
					formMan.loadRecord(selected[0]);
        			winMan.show();
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[/*{
        		xtype:'button',
        		text:'测试-需要修改为TAB激活',
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
        			var ids="";// 合同ID
        			Ext.each(selected, function (item) {
        				ids+=item.data.id;
        			});
        			//alert(ids) ;
        			Ext.getCmp('formMan').getForm().setValues({training_contract_id:ids});
        			var params = {training_contract_id: ids};
        		    Ext.apply(storeMan.proxy.extraParams, params);
        		    storeMan.load();
        		}
        	},{
        		xtype:'button',
        		text:'添加',
        		handler:function(){
        			formMan.getForm().reset();
        			// 设置合同ID
        			var selected = grid.getSelectionModel().getSelection();
        			var ids="";// 合同ID
        			Ext.each(selected, function (item) {
        				ids+=item.data.id;
        			});
        			//alert(ids) ;
        			Ext.getCmp('formMan').getForm().setValues({training_contract_id:ids});
        			winMan.show();
        		}
        	},*/{
        		xtype:'button',
        		text:'详情',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = gridMan.getSelectionModel().getSelection();
        			if(selected!=''){
        				formMan.getForm().reset();
    					formMan.loadRecord(selected[0]);
            			winMan.show();
        			}else{
        				Ext.Msg.alert('系统提示', '请选择需要查看详情的合同签订人!');
        			}
        		}
        	}/*,{
        		xtype:'button',
        		text:'删除',
        		handler:function(){
        			var selected = gridMan.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
	        				if(btn=='yes'){
	        					var ids="";
	                			Ext.each(selected, function (item) {
	                				ids+=item.data.id+",";
	                			});
	                			Ext.Ajax.request({
	                			    url:'/customercontrol/training_contract_mans_del/',
	                			    method : 'post',
	                			    params:{
	                			    	ids:ids
	                			    },
	                			    success:function(response){
	                			        var results =  Ext.decode(response.responseText);
	                			        if(results.success){
	                			        	storeMan.load();
	                			        }else{
	                			        	Ext.Msg.alert('系统提示', results.message);
	                			        }
	                			    }
	                			});
	        				}
	        			});
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要删除的合同签订人!');
	    			}

        		}
        	}*/]
        }]
    });
    
	var formMan = Ext.create('Ext.form.Panel', {
		id:'formMan',
		width: 350,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'id',
			name:'id'
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'hiddenfield',
			items:[ {
				fieldLabel : '合同号',
				name : 'training_contract_id',
				allowBlank : false,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ employeComboBox/*{
				fieldLabel : '员工',
				name : 'employe_id',
				allowBlank : false,
				blankText : '请输入'
			}*/]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '提成比例%',
				name : 'ratio',
				allowBlank : false,
				blankText : '请输入'
			}]
		}]
		
	});

	
	var winMan = Ext.create('Ext.window.Window', {
		title : '合同明细',
		modal: true,
		closeAction: 'hide',
		items : [formMan],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ /*{
			text : '保存',
			handler : function() {
				var tid = Ext.getCmp('formMan').getForm().findField('training_contract_id').getValue();
				var eid = Ext.getCmp('employe_combobox_id').getValue();
				Ext.Ajax.request({
				    url:'/customercontrol/training_contract_mans_list/',
				    method : 'post',
				    params:{training_contract_id:tid,start:0,limit:20},
				    success:function(response){
				        var results =  Ext.decode(response.responseText);
				        var rows = results.rows ;
				        var len = rows.length ;
				        var flag = 0 ;
				        var ratio = 0 ;
				        for(var i=0 ; i<len ; i++){
				        	ratio += parseInt(rows[i].ratio) ;
				        	if(rows[i].employe_id==eid){
				        		parseInt(flag)++ ;
				        	}
				        }
				        
				        
				        // 相同员工没有提成比例
				        if(flag==0){
				        	if (formMan.getForm().isValid()){
					        	// 同一合同下，提成比例不超过100
				        		ratio += parseInt(Ext.getCmp('formMan').getForm().findField('ratio').getValue()) ;
					        	if (ratio<=100){
					        		formMan.getForm().submit({
										waitTitle : '请稍候',
										waitMsg : '正在处理......',
										url : '/customercontrol/training_contract_mans_save/',
										success : function(form, action) {
											winMan.hide();
											storeMan.load();
										},
										failure : function(form, action) {
											if (action.result) {
												Ext.Msg.alert('系统提示', action.result.message);
											} 
										}
									});
									
					        	}
								else{
									Ext.Msg.alert('系统提示', '该合同下提成比例已超过100%!');
								}
				        	}	
				        }else{
				        	Ext.Msg.alert('系统提示', '该员工已存在提成比例!');
				        }
				        
				    }
				});
			
			}
		},*/ {
			text : '确定',
			handler : function() {
				formMan.getForm().reset();
				winMan.hide();
			}
		} ]
	});
	
	

	
	
	
	Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'customer-contract-audit-finance-tab',
		items:[Ext.create('Ext.tab.Panel',{
			//renderTo: 'customer-contract-audit-tab',
			activeTab: 0,
			items:[grid,gridDetail,gridMan],
			listeners:{
				'tabchange':function(tabPanel, newCard,oldCard,eOpts){
					if(newCard==gridDetail){
						var selected = grid.getSelectionModel().getSelection();
	        			var ids="";// 合同ID
	        			var sids="";// 学生ID
	        			Ext.each(selected, function (item) {
	        				ids+=item.data.id;
	        				sids+=item.data.student_id;
	        			});

	        			Ext.getCmp('formDetail').getForm().setValues({training_contract_id:ids});
	        			Ext.getCmp('formDetail').getForm().setValues({student_id:sids});
	        			var params = {training_contract_id: ids};
	        		    Ext.apply(storeDetail.proxy.extraParams, params);
	        		    storeDetail.load();
					}else if(newCard==gridMan){
						var selected = grid.getSelectionModel().getSelection();
	        			var ids="";// 合同ID
	        			Ext.each(selected, function (item) {
	        				ids+=item.data.id;
	        			});

	        			Ext.getCmp('formMan').getForm().setValues({training_contract_id:ids});
	        			var params = {training_contract_id: ids};
	        		    Ext.apply(storeMan.proxy.extraParams, params);
	        		    storeMan.load();
					}else{// 主grid
						gridDetail.disable() ;
						gridMan.disable() ;
					}

					
				}
			}
		})]
	});
	
	
});